package com.room.job;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.cyh.dao.SysConstant;
import com.cyh.dao.entity.Report;
import com.cyh.dao.entity.Reserve;
import com.cyh.dao.entity.SysUser;
import com.room.service.ReportService;
import com.room.service.ReserveService;
import com.room.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class Job {

    @Autowired
    private ReserveService reserveService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private ReportService reportService;

    @Scheduled(fixedRate = 3600000)
    @Transactional(rollbackFor = Exception.class)
    public void job() throws Exception {
        List<Reserve> list = reserveService.list(new QueryWrapper<Reserve>()
                .eq("invalid_is", 0));
        int hour = LocalDateTime.now().getHour();
        if (CollectionUtils.isEmpty(list)){
            return;
        }
        List<SysUser> sysUsers = sysUserService.list(new LambdaQueryWrapper<SysUser>()
            .in(SysUser::getId, list.stream().map(Reserve::getUserId)
            .collect(Collectors.toSet())));
        for (Reserve reserve : list) {
            SysUser user = new SysUser();
            for (SysUser u : sysUsers) {
                if (u.getId().equals(reserve.getUserId())){
                    user = u;
                    break;
                }
            }
            if (SysConstant.SJD_A.equals(reserve.getTimeInterval())){
                if (hour>= 12){
                    reserve.setInvalidIs(1);
                    user.setLearnLen(user.getLearnLen() + 4);
                }
            }else if (SysConstant.SJD_B.equals(reserve.getTimeInterval())){
                if (hour>= 18){
                    reserve.setInvalidIs(1);
                    user.setLearnLen(user.getLearnLen() + 4);
                }
            }else if (SysConstant.SJD_C.equals(reserve.getTimeInterval())){
                if (hour>= 22){
                    reserve.setInvalidIs(1);
                    user.setLearnLen(user.getLearnLen() + 3);
                }
            }
        }
        sysUserService.updateBatchById(sysUsers);
        reserveService.updateBatchById(list);
    }

    @Scheduled(fixedRate = 1800000)
    @Transactional(rollbackFor = Exception.class)
    public void job2() throws Exception {
        List<Report> list = reportService.list(new LambdaQueryWrapper<Report>()
                .eq(Report::getReportUserBack, 0));
       if (CollectionUtils.isNotEmpty(list)){
           LocalDateTime now = LocalDateTime.now();
           for (Report report : list) {
               Duration between = Duration.between(report.getReportTime(), now);
               if (between.toMinutes() >= 30){
                   report.setReportUserBack(2);
               }
           }
           reportService.updateBatchById(list);
       }
    }

}
